Skip to content

Set Matrix Zeroes ​

Set Matrix Zeroes — LeetCode

If any element of an m x n matrix is 0, set its entire row and column to 0, doing it in place with O(1) extra space.

Approach ​

Use the first row and first column and an additional variable to denote what rows to set zero to.

Then, set the first row and first column to zero if any of the other elements in their row or column is zero.

Iterate over the elements except first row and first column and set them to zero.

Separately iterate over the first row and first column and set them to zero

Remarks ​

Pasted image 20250903002924.png

https://www.geeksforgeeks.org/dsa/set-matrix-rows-and-columns-to-zeroes/